package com.student.servlet;

import com.student.dao.StudentDAO;
import com.student.model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentDAO studentDAO = new StudentDAO();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        if (action == null) {
            action = "list";
        }

        switch (action) {
            case "list":
                list(request, response);
                break;
            case "edit":
                showEditForm(request, response);
                break;
            case "delete":
                deleteStudent(request, response);
                break;
            default:
                list(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");

        switch (action) {
            case "add":
                addStudent(request, response);
                break;
            case "update":
                updateStudent(request, response);
                break;
        }
    }


    private void list(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String studentNumber = request.getParameter("studentNumber");
        String name = request.getParameter("name");
        String major = request.getParameter("major");

        List<Student> students;
        if (studentNumber != null || name != null || major != null) {
            students = studentDAO.search(studentNumber, name, major);
        } else {
            students = studentDAO.findAll();
        }

        request.setAttribute("studentList", students);
        request.getRequestDispatcher("student-list.jsp").forward(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Student student = studentDAO.findById(id);
        request.setAttribute("student", student);
        request.getRequestDispatcher("student-edit.jsp").forward(request, response);
    }

    private void addStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Student student = new Student();
            student.setStudentNumber(request.getParameter("studentNumber"));
            student.setName(request.getParameter("name"));
            student.setGender(request.getParameter("gender"));
            student.setAge(Integer.parseInt(request.getParameter("age")));
            student.setMajor(request.getParameter("major"));
            student.setClassName(request.getParameter("className"));
            student.setAdmissionDate(dateFormat.parse(request.getParameter("admissionDate")));
            student.setPhone(request.getParameter("phone"));
            student.setEmail(request.getParameter("email"));
            student.setAddress(request.getParameter("address"));

            if (studentDAO.add(student)) {
                response.sendRedirect("student?action=list");
            } else {
                request.setAttribute("error", "添加学生失败");
                request.getRequestDispatcher("student-add.jsp").forward(request, response);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            request.setAttribute("error", "日期格式错误");
            request.getRequestDispatcher("student-add.jsp").forward(request, response);
        }
    }

    private void updateStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Student student = new Student();
            student.setId(Integer.parseInt(request.getParameter("id")));
            student.setStudentNumber(request.getParameter("studentNumber"));
            student.setName(request.getParameter("name"));
            student.setGender(request.getParameter("gender"));
            student.setAge(Integer.parseInt(request.getParameter("age")));
            student.setMajor(request.getParameter("major"));
            student.setClassName(request.getParameter("className"));
            student.setAdmissionDate(dateFormat.parse(request.getParameter("admissionDate")));
            student.setPhone(request.getParameter("phone"));
            student.setEmail(request.getParameter("email"));
            student.setAddress(request.getParameter("address"));

            if (studentDAO.update(student)) {
                response.sendRedirect("student?action=list");
            } else {
                request.setAttribute("error", "更新学生信息失败");
                request.setAttribute("student", student);
                request.getRequestDispatcher("student-edit.jsp").forward(request, response);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            request.setAttribute("error", "日期格式错误");
            request.getRequestDispatcher("student-edit.jsp").forward(request, response);
        }
    }

    private void deleteStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        studentDAO.delete(id);
        response.sendRedirect("student?action=list");
    }
}
